package org.cis.jproyinv.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class ServicioDao<E, K> implements Dao<E, K> {

    private EntityManager em;
    private EntityManagerFactory emf;

    public ServicioDao() {
        emf = Persistence.createEntityManagerFactory("jproyinvPU");
        em = emf.createEntityManager();
    }

    public void guardar(E entidad) {
        getEm().getTransaction().begin();
        getEm().persist(entidad);
        getEm().getTransaction().commit();
    }

    public void actualizar(E entidad) {
        getEm().getTransaction().begin();
        getEm().remove(getEm().merge(entidad));
        getEm().getTransaction().commit();
        getEm().getTransaction().begin();
        getEm().persist(entidad);
        getEm().getTransaction().commit();
    }

    public void eliminar(E entidad) {
        getEm().getTransaction().begin();
        getEm().remove(getEm().merge(entidad));
        getEm().getTransaction().commit();
    }

    public E buscar(K id) {
        return null;
    }

    public List<E> buscarTodos() {
        return null;
    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }
}
